﻿
package com.ostrich.project.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.base.util.StringUtil;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.common.ImportUtil;
import com.ostrich.manpower.service.impl.TostHrBaseinfoService;
import com.ostrich.project.bean.TostPjMempushmoney;
import com.ostrich.project.service.TostPjMempushmoneyDao;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostPjMempushmoneyService")
public class TostPjMempushmoneyService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostPjMempushmoneyService.class);
	private String hrname;
	
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrBaseinfoService<TostHrBaseinfo> tostHrBaseinfoService; 
	
	@Autowired
    private TostPjMempushmoneyDao<T> dao;

	public TostPjMempushmoneyDao<T> getDao() {
		return dao;
	}
	
	public List<TostPjMempushmoney> queryBypjIdAndMohth(int pjId,String startTime,String endTime){
		return getDao().queryBypjIdAndMohth(pjId, startTime, endTime);
	}
	
	public List<TostPjMempushmoney> queryByhrId(int pjId,int hrId,String startTime,String endTime){
		return getDao().queryByhrId(pjId,hrId, startTime, endTime);
	}
	
	public List<TostPjMempushmoney> queryByMpmMonth(int pjId,String startTime,String endTime){
		return getDao().queryByMpmMonth(pjId, startTime, endTime);
	}
	
	public Map<String,Object> resultajax(TostPjMempushmoney tostPjMempushmoney,String startTime,String endTime){
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		List<TostPjMempushmoney> monthList = this.queryByMpmMonth(tostPjMempushmoney.getPjId(), startTime, endTime);
		List<String> months = new ArrayList<String>();
		for(int m = 0;m<monthList.size();m++){
			months.add(monthList.get(m).getMpmMonth());
		}
		jsonMap.put("months",months);
		List<String> copyMonths = new ArrayList<String>();
		copyMonths.addAll(months);
		
		List<TostPjMempushmoney> dataList = this.queryBypjIdAndMohth(tostPjMempushmoney.getPjId(),startTime, endTime);
		jsonMap.put("listSize", dataList.size());
		List<TostPjMempushmoney> hrList = null;
		List<String> hrmonths = new ArrayList<String>();
		int index = 0;
		for(int i=0;i<dataList.size();i++){
			hrList = this.queryByhrId(tostPjMempushmoney.getPjId(), dataList.get(i).getHrId(),startTime, endTime);
			for(int h = 0;h<hrList.size();h++){
				hrmonths.add(hrList.get(h).getMpmMonth());
			}
			months.removeAll(hrmonths);
			for(int j = 0;j<months.size();j++){
				TostPjMempushmoney memp = new TostPjMempushmoney();
				memp.setHrName(hrList.get(0).getHrName());
				index = copyMonths.indexOf(months.get(j));
				memp.setMpmMonth(months.get(j));
				memp.setMpmPycount(new Float(0));
				hrList.add(index, memp);
			}
			jsonMap.put("dataList_"+i,hrList);
			hrmonths.removeAll(hrmonths);
			months.removeAll(months);
			months.addAll(copyMonths);
			hrList = null;
		}
		return jsonMap;
	}
	
	public void setMempushMoneyBySheet(HSSFSheet sheet){
	    HSSFRow row = null;
	    int colnum=13;
		 //遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数  除去标题跟表头
	     for(int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
	         row = sheet.getRow(j);
	         TostPjMempushmoney  memPushMoney = new TostPjMempushmoney();
	         for(int i=0;i<colnum;i++ ){
	        	 String str = ImportUtil.getCellValue(row.getCell(i));
	        	 if(i==1)//项目编号
	        		 memPushMoney.setPjId(Integer.parseInt(str));
	        	 else if(i==2){//费用编号
	        		 memPushMoney.setPctId(Integer.parseInt(str));
	        	 }else if(i==3){//项目名称
	        		 memPushMoney.setPjName(str);
	        	 }else if(i==4){//人员id
	        		 hrname = tostHrBaseinfoService.queryHrnameByid(Integer.parseInt(str));
	        		 memPushMoney.setHrId(Integer.parseInt(str));
	        	 }else if(i==5){//人员姓名
	        		 memPushMoney.setHrName(hrname);
	        	 }else if(i==6){//开始时间
	        		 memPushMoney.setMpmEntertime(str);
	        	 }else if(i==7){//月数
	        		 memPushMoney.setMpmMonnum(Integer.parseInt(str));
	        	 }else if(i==8){
	        		 memPushMoney.setMpmMonth(str);
	        	 } else if(i==9){//提成金额
	        		 Float flstr = (str!=null  && str.trim().length()>0 && StringUtil.isNumeric(str))?Float.parseFloat(str):new Float(0);
	        		 memPushMoney.setMpmPushmoney(flstr);
	        	 }else if(i==10){//提成合计
	        		 Float flstr = (str!=null  && str.trim().length()>0 && StringUtil.isNumeric(str))?Float.parseFloat(str):new Float(0);
	        		 memPushMoney.setMpmPycount(flstr);
	        	 }else if(i==11){//创建时间
	        		 memPushMoney.setCreatetime(str);
	        	 }else if(i==12){//创建人
	        		 memPushMoney.setPerson(str);
	        	 } 
	         }
        	 dao.add((T)memPushMoney);
	     }
		
	}
}
